package com.jstyle.jstylecommon.utils;

import android.util.Log;

import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2017/4/6.
 */

public class ResolveData {
    private static final String TAG = "ResolveData";
    private static final int SHORTENED_LOCAL_NAME = 0x08;
    private static final int COMPLETE_LOCAL_NAME = 0x09;
    private static final long oneMinMillis = 60 * 1000l;

    public static int getBcd(String value) {
        Integer m = Integer.parseInt(value, 16);
        return m.intValue();
    }

    public static String decodeDeviceName(byte[] data) {
        String name = null;
        int fieldLength, fieldName;
        int packetLength = data.length;
        for (int index = 0; index < packetLength; index++) {
            fieldLength = ResolveData.getValue(data[index],0);
            if (fieldLength == 0)
                break;
            fieldName =ResolveData.getValue(data[++index],0) ;

            if (fieldName == COMPLETE_LOCAL_NAME
                    || fieldName == SHORTENED_LOCAL_NAME) {
                name = decodeLocalName(data, index + 1, fieldLength - 1);
                break;
            }
            index += fieldLength - 1;
        }
        return name;
    }

    /**
     * Decodes the local name
     */
    public static String decodeLocalName(final byte[] data, final int start,
                                         final int length) {
        try {
            return new String(data, start, length, "UTF-8");
        } catch (final UnsupportedEncodingException e) {
            Log.e("scan", "Unable to convert the complete local name to UTF-8",
                    e);
            return null;
        } catch (final IndexOutOfBoundsException e) {
            Log.e("scan", "Error when reading complete local name", e);
            return null;
        }
    }

    /**
     * 转十六进制字符串
     *
     * @param data
     * @return
     */
    public static String byte2Hex(byte[] data) {
        if (data != null && data.length > 0) {
            StringBuilder sb = new StringBuilder(data.length);
            for (byte tmp : data) {
                sb.append(String.format("%02X ", tmp));
            }
            return sb.toString();
        }
        return "no data";
    }



    public static int getValue(byte b, int count) {
        return (int) ((b & 0xff) * Math.pow(256, count));
    }









    public static final String Default_FormatString="yy.MM.dd";
    static SimpleDateFormat format = new SimpleDateFormat("yy.MM.dd HH:mm:ss");
    static SimpleDateFormat defaultFormat = new SimpleDateFormat(Default_FormatString);
    /**
     * 解析获取历史数据时间段
     *
     * @param time
     * @return
     */
    public static synchronized String getHistoryTime(long time) {
        long defaultTime = getDateLong("00.01.01");
        return getFormatTimeString(time * 1000 + defaultTime);
    }
    public static synchronized String getFormatTimeString(long time) {
        return format.format(new Date(time));
    }
    /**字符串代表日期的毫秒数
     * @param date
     * @return
     */
    public static synchronized long getDateLong(String date) {

        long time = 0;
        try {
            Date dates = defaultFormat.parse(date);
            time = dates.getTime();
        } catch (Exception e) {
            // TODO: handle exception
        }
        return time;
    }
    /**
     * 获取从2000.1.1到现在的秒数值
     *
     * @param value
     * @param start
     * @return
     */
    public static long getTimeFrom2000(byte[] value, int start) {
        long time = 0;
        for (int i = 0; i < 4; i++) {
            time += getValue(value[start + i], i);
        }
        return time;
    }







//    public static TurnOverData saveTurnOverData(String address, byte[] value, int timeCount, int turnOverCount) {
//        String time = getHistoryTime(getTimeFrom2000(value, timeCount));
//        long turnOverTime = getTimeFrom2000(value, turnOverCount);
//        // Log.i(TAG, "saveTurnOverData: " + time + " " + turnOverTime);
//        TurnOverData turnOverData = new TurnOverData();
//        turnOverData.setAddress(address);
//        turnOverData.setId(getTimeFrom2000(value, timeCount));
//        turnOverData.setTime(time);
//        turnOverData.setPeriodTime(turnOverTime);
//        return turnOverData;
//        //TurnOverDataDaoManager.insertTurnOverData(turnOverData);
//    }





}
